用SQL创建学生成绩数据库

您所在的位置:网站首页 sql server建立数据库 用SQL创建学生成绩数据库

用SQL创建学生成绩数据库

2023-04-02 11:50| 来源: 网络整理| 查看: 265

方法一:创建数据库school,这个数据库中包含四个表:分别是学生表、教师表、课程表和成绩表。语法:create database school;(创建数据库school) show databases;(查看是否已经创建好) drop database school;(删除数据库school,这里不必删除)

2.设计创建学生表、教师表、课程表和成绩表。

语法: use school; create table student( sno varchar(20) not null, sname varchar(20) not null, ssex varchar(20) not null default'男', sbirthday datetime, sclass varchar(20));语法:use school; create table teacher( tno varchar(20) not null, tname varchar(20) not null, tsex varchar(20) not null default'男', tbirthday datetime, prof varchar(20) not null, depart varchar(20));语法:use school; create table course( cno varchar(20) not null, cname varchar(20) not null, tno varchar(20) not null);语法:use school; create table score( sno varchar(20) not null, cno varchar(20) not null, degree decimal(4,1) not null);

显示所有表是否已经创建,可用以下语句:

Show tables from school;

3.添加主键和外键约束

语法:

对学生表添加主键约束:

alter table student add constraint primary key(sno);

对成绩表添加外键约束:

alter table student add constraint foreign key(sno) references student(sno); alter table student add constraint foreign key(cno) references course(cno);

对成绩表添加主键约束:

Alter table student Add constraint primary key(sno,cno);

用语法将以上的主键和外键都添加约束:

alter table student add constraint primary key(sno); alter table teacher add constraint primary key(tno); alter table course add constraint primary key(cno); alter table course add constraint foreign key(tno) references teacher(tno); alter table score add constraint primary key(sno,cno); alter table score add constraint foreign key(cno) references course(cno);

4.将以下学生信息插入到学生表中:

语法:insert into student(sno,sname,ssex,sbirthday,sclass) values(108,'曾华','男','1997-09-01',95033); insert into student(sno,sname,ssex,sbirthday,sclass) values(105,'匡明','男','1975-10-02',95031); insert into student(sno,sname,ssex,sbirthday,sclass) values(107,'王丽','女','1976-01-23',95033); insert into student(sno,sname,ssex,sbirthday,sclass) values(101,'李军','男','1976-02-20',95033); insert into student(sno,sname,ssex,sbirthday,sclass) values(109,'王芳','女','1975-02-10',95031); insert into student(sno,sname,ssex,sbirthday,sclass) values(103,'陆君','男','1974-06-03',95031); select*from student;(查看是否所有信息都插入表中了)

结果如下:

5.将以下信息插入到教师表中:

语法: insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(804,'李诚','男','1958-12-02','副教授','计算机系'); insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(856,'张旭','男','1969-03-12','讲师','电子工程系'); insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(825,'王萍','女','1972-05-05','助教','计算机系'); insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(831,'刘冰','女','1977-08-14','助教','电子工程系');

6.以下信息输入课程表表中

语法:insert into course(cno,cname,tno) values('3-105','计算机导论',825); insert into course(cno,cname,tno) values('3-245','操作系统',804); insert into course(cno,cname,tno) values('6-166','数据电路',856); insert into course(cno,cname,tno) values('19-888','高等数学',831);

7.将以下信息输入成绩表中:

语法:insert into score(sno,cno,degree) values(103,'3-245',86); insert into score(sno,cno,degree) values(105,'3-245',75); insert into score(sno,cno,degree) values(109,'3-245',68); insert into score(sno,cno,degree) values(103,'3-105',92); insert into score(sno,cno,degree) values(105,'3-105',88); insert into score(sno,cno,degree) values(109,'3-105',76); insert into score(sno,cno,degree) values(101,'3-105',64); insert into score(sno,cno,degree) values(107,'3-105',91); insert into score(sno,cno,degree) values(108,'3-105',78); insert into score(sno,cno,degree) values(101,'6-166',85); insert into score(sno,cno,degree) values(107,'6-166',79); insert into score(sno,cno,degree) values(108,'6-166',81);

方法二:

以上1-7的创建数据库、在数据库中创建表、为表添加数据,也可通过以下步骤实现:

创建数据库【学生成绩数据库】,这个数据库中包含四个表:分别是【学生表】、【教师表】、【课程表】和【成绩表】。

这四个表的联系如下:

具体操作如下:打开navicat后,在【daxy】鼠标右键,选择新建数据库,并命名为【学生成绩数据库】

第二步:创建好数据库后,开始创建:学生表、教师表、课程表和成绩表。

操作如下:

鼠标右键点击【学生成绩数据库】-【打开数据库】,当【学生成绩数据库】前面的正方形图标由灰变绿时,表示数据库已打开,这时点击正方形前面的方向键,然后看到【表】,鼠标右键【表】-【新建表】。

创建学生表,具体操作如下:通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【sno】设置为主键,【sbirthday】的类型设置为【date】。最后点击保存,即可得到学生表【student】。

创建教师表,具体操作如下:鼠标右键【表】-【新建表】,后点击通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【tno】设置为主键,【tbirthday】的类型设置为【date】,最后点击保存,即可得到教师表【teacher】。

创建课程表,具体操作如下:鼠标右键【表】-【新建表】,后通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【cno】设置为主键 ,最后点击保存,即可得到课程表【course】。

创建成绩表,具体操作如下:鼠标右键【表】-【新建表】,后通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【cno】设置为主键 ,最后点击保存,即可得到课程表【score】。

这时,四张表已经创建完毕,接下来可以为这四张表添加数据了。

为【student】表添加数据,具体方法为:点击【查询】-【新建查询】。

运行后,若无语法错误,可点击【保存】-【输入查询名】:student:添加数据。

然后点击【表】-【student】-【打开表】,即可看到添加的数据:

以上用到的sql语法如下:

-- 为student表添加数据 insert into student(sno,sname,ssex,sbirthday,sclass) values(108,'曾华','男','1997-09-01',95033); insert into student(sno,sname,ssex,sbirthday,sclass) values(105,'匡明','男','1975-10-02',95031); insert into student(sno,sname,ssex,sbirthday,sclass) values(107,'王丽','女','1976-01-23',95033); insert into student(sno,sname,ssex,sbirthday,sclass) values(101,'李军','男','1976-02-20',95033); insert into student(sno,sname,ssex,sbirthday,sclass) values(109,'王芳','女','1975-02-10',95031); insert into student(sno,sname,ssex,sbirthday,sclass) values(103,'陆君','男','1974-06-03',95031);

为【teacher】表添加数据,具体方法为:点击【查询】-【新建查询】,运行后,若无语法错误,可点击【保存】-【输入查询名】:【teacher:添加数据】。

然后再点击【表】-【teacher】-【打开表】,即可查看添加的数据。

以上用到的sql语法如下:

-- 为teacher表添加数据 insert into teacher (tno,tname,tsex,tbirthday,prof,depart) values(804,'李诚','男','1958-12-2','副教授','计算机系'); insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(856,'张旭','男','1969-03-12','讲师','电子工程系'); insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(825,'王萍','女','1972-05-05','助教','计算机系'); insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(831,'刘冰','女','1977-08-14','助教','电子工程系');

为【course】表添加数据,具体方法为:点击【查询】-【新建查询】,运行后,若无语法错误,可点击【保存】-【输入查询名】:【course:添加数据】。

然后再点击【表】-【course】-【打开表】,即可查看添加的数据。

以上用到的sql语法如下:

-- 为course表添加数据 insert into course(cno,cname,tno) values('3-105','计算机导论',825); insert into course(cno,cname,tno) values('3-245','操作系统',804); insert into course(cno,cname,tno) values('6-166','数据电路',856); insert into course(cno,cname,tno) values('19-888','高等数学',831);

为【score】表添加数据,具体方法为:点击【查询】-【新建查询】,运行后,若无语法错误,可点击【保存】-【输入查询名】:【score:添加数据】。

然后再点击【表】-【score】-【打开表】,即可查看添加的数据。

以上用到的sql语法如下:

-- 为score表添加数据 insert into score(sno,cno,degree) values(103,'3-245',86); insert into score(sno,cno,degree) values(105,'3-245',75); insert into score(sno,cno,degree) values(109,'3-245',68); insert into score(sno,cno,degree) values(103,'3-105',92); insert into score(sno,cno,degree) values(105,'3-105',88); insert into score(sno,cno,degree) values(109,'3-105',76); insert into score(sno,cno,degree) values(101,'3-105',64); insert into score(sno,cno,degree) values(107,'3-105',91); insert into score(sno,cno,degree) values(108,'3-105',78); insert into score(sno,cno,degree) values(101,'6-166',85); insert into score(sno,cno,degree) values(107,'6-166',79); insert into score(sno,cno,degree) values(108,'6-166',81);

以上方法二选一即可。

8.要求查询学生表中的sname、ssex和sclass列。

语法:select sname,ssex,sclass from student;

9.查询学生表中的所有记录。

语法:

Select*from student;

结果如下:

10.查询教师表中不同的系名,以及每个系有多少老师。(重点)

分析:

按系名分组(group by)求每组有多少老师,即每组有几行?(count)语法:Select depart,count(*) From teacher Group by depart

11.查询成绩表中分数在60分到80分之间的所有记录

分析:

查询成绩表中的某些记录 这些记录一定满足60分到80分语法:Select*from score Where degree between 60 and 80;

12.查询成绩表中分数为85,86或88的记录。

分析:

查询成绩表中的某些记录 这些记录应满足成绩为85,86或88

这里用where子句:

方法a:使用关键字in

方法b:使用关键字or

语法a:Select* from score Where degree in(85,86,88);

语法b: Select* from score Where degree =85 or degree =86 or degree =88;

注:这里的or表示并且,不能用and(and表示交集)

13.查询学生表中“95031”班或性别为“女”的记录。

分析:

查询某些同学的记录 where子句筛选符合条件的行语法a:select* from student where sclass='95031' or ssex='女';语法b:查询95031这个班级的所有同学的记录查询性别为女的所有记录用关键字union连接以上的查询语句。select* from student where sclass='95031' union select* from student where ssex='女';

14.查询所有不姓王的同学记录。

SQL语句: select* from student where sname not like '王%';

15.以class降序来查询所有学生记录。

分析:

查询所有学生记录用order by子句对查询结果进行降序排序SQL语句: select* from student Order by sclass desc;以上1-7,创建数据库、添加表、添加数据可用以下方法实现:

要求创建数据库【学生成绩数据库】,这个数据库中包含四个表:分别是【学生表】、【教师表】、【课程表】和【成绩表】。

这四个表的联系如下:

具体操作如下:打开navicat后,在【daxy】鼠标右键,选择新建数据库,并命名为【学生成绩数据库】

第二步:创建好数据库后,开始创建:学生表、教师表、课程表和成绩表。

操作如下:

鼠标右键点击【学生成绩数据库】-【打开数据库】,当【学生成绩数据库】前面的正方形图标由灰变绿时,表示数据库已打开,这时点击正方形前面的方向键,然后看到【表】,鼠标右键【表】-【新建表】。

创建学生表,具体操作如下:通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【sno】设置为主键,【sbirthday】的类型设置为【date】。最后点击保存,即可得到学生表【student】。

创建教师表,具体操作如下:鼠标右键【表】-【新建表】,后点击通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【tno】设置为主键,【tbirthday】的类型设置为【date】,最后点击保存,即可得到教师表【teacher】。

创建课程表,具体操作如下:鼠标右键【表】-【新建表】,后通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【cno】设置为主键 ,最后点击保存,即可得到课程表【course】。

创建成绩表,具体操作如下:鼠标右键【表】-【新建表】,后通过【添加字段】一一添加好字段,并根据四张表之间的关系,将【cno】设置为主键 ,最后点击保存,即可得到课程表【score】。

这时,四张表已经创建完毕,接下来可以为这四张表添加数据了。

为【student】表添加数据,具体方法为:点击【查询】-【新建查询】。

运行后,若无语法错误,可点击【保存】-【输入查询名】:student:添加数据。

然后点击【表】-【student】-【打开表】,即可看到添加的数据:

以上用到的sql语法如下:

-- 为student表添加数据

insert into student(sno,sname,ssex,sbirthday,sclass)

values(108,'曾华','男','1997-09-01',95033);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(105,'匡明','男','1975-10-02',95031);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(107,'王丽','女','1976-01-23',95033);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(101,'李军','男','1976-02-20',95033);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(109,'王芳','女','1975-02-10',95031);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(103,'陆君','男','1974-06-03',95031);

为【teacher】表添加数据,具体方法为:点击【查询】-【新建查询】,运行后,若无语法错误,可点击【保存】-【输入查询名】:【teacher:添加数据】。

然后再点击【表】-【teacher】-【打开表】,即可查看添加的数据。

以上用到的sql语法如下:

-- 为teacher表添加数据

insert into teacher (tno,tname,tsex,tbirthday,prof,depart)

values(804,'李诚','男','1958-12-2','副教授','计算机系');

insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(856,'张旭','男','1969-03-12','讲师','电子工程系');

insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(825,'王萍','女','1972-05-05','助教','计算机系');

insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(831,'刘冰','女','1977-08-14','助教','电子工程系');

为【course】表添加数据,具体方法为:点击【查询】-【新建查询】,运行后,若无语法错误,可点击【保存】-【输入查询名】:【course:添加数据】。

然后再点击【表】-【course】-【打开表】,即可查看添加的数据。

以上用到的sql语法如下:

-- 为course表添加数据

insert into course(cno,cname,tno)

values('3-105','计算机导论',825);

insert into course(cno,cname,tno)

values('3-245','操作系统',804);

insert into course(cno,cname,tno)

values('6-166','数据电路',856);

insert into course(cno,cname,tno)

values('19-888','高等数学',831);

为【score】表添加数据,具体方法为:点击【查询】-【新建查询】,运行后,若无语法错误,可点击【保存】-【输入查询名】:【score:添加数据】。

然后再点击【表】-【score】-【打开表】,即可查看添加的数据。

以上用到的sql语法如下:

-- 为score表添加数据

insert into score(sno,cno,degree)

values(103,'3-245',86);

insert into score(sno,cno,degree)

values(105,'3-245',75);

insert into score(sno,cno,degree)

values(109,'3-245',68);

insert into score(sno,cno,degree)

values(103,'3-105',92);

insert into score(sno,cno,degree)

values(105,'3-105',88);

insert into score(sno,cno,degree)

values(109,'3-105',76);

insert into score(sno,cno,degree)

values(101,'3-105',64);

insert into score(sno,cno,degree)

values(107,'3-105',91);

insert into score(sno,cno,degree)

values(108,'3-105',78);

insert into score(sno,cno,degree)

values(101,'6-166',85);

insert into score(sno,cno,degree)

values(107,'6-166',79);

insert into score(sno,cno,degree)

values(108,'6-166',81);



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3